/*
*报表填写
 */
var pageCurr;
var form;
$(function() {

    layui.use(['table','laydate'], function(){
        var laydate = layui.laydate;
        var table = layui.table;
        form = layui.form;
        //执行一个laydate实例
        laydate.render({
            elem: '#year' //指定元素
            ,type: 'year'
        });
        $("#year").val(new Date().getFullYear());
        var industryId = $("#industryId").val();
        var year = $("#year").val();
        var tempTitle = $("#tempTitle").val();
        tableIns=table.render({
            id:'id',
            elem: '#reportList',
            url:'/report/reportList?industryId='+industryId,
            method: 'post', //默认：get请求
            where :{
                "year":year,
                "tempTitle":tempTitle
              },
            cellMinWidth: 80,
            page: true,
            height: 'full-'+270,
            request: {
                pageName: 'pageNum', //页码的参数名称，默认：pageNum
                limitName: 'pageSize' //每页数据量的参数名，默认：pageSize
            },
            response:{
                statusName: 'code', //数据状态的字段名称，默认：code
                statusCode: 200, //成功的状态码，默认：0
                countName: 'totals', //数据总数的字段名称，默认：count
                dataName: 'list' //数据列表的字段名称，默认：data
            },
            cols: [[

                {field:'industryName', title:'行业名称',align:'center'/*,width:"10%"*/}
                ,{field:'tempTitle',title:'模板标题',align:'center'}
                ,{field:'industryId',hide:'true'}
                ,{field:'reportId',hide:'true'}
                ,{field:'tempId',hide:'true'}
                ,{field:'userID',hide:'true'}
                ,{field:'userName', title:'填写人',align:'center'/*,width:"10%"*/}
                ,{field:'writeTime', title:'填写时间',align:'center'/*,width:"10%"*/}
                ,{field:'status', title:'状态',align:'center',templet:function (d) {
                    console.log($(this))
                        $(this).parent().css("color","red");
                        if(d.status ==0){
                            return "<div style='color: red'>未填写</div>";
                        }else if (d.status ==1){
                            return "<div style='color: green'>已填写</div>";
                        }
                    },width:"10%"}
                ,{fixed:'right',title:'操作',align:'left', toolbar:'#optBar',width:"7%"}
            ]],
            done: function(res, curr, count){

            }
        });


        //监听工具条
        table.on('tool(reportTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'write'){
               write(data);
            }
        });

        form.on('submit(searchSubmit)', function(data){
            //重新加载table
            load(data);
            return false;
        });
    });

});

function write(data) {
    var form = $("<form method='post'></form>");
    var input;
    form.attr({"action":"/write/writePage"});
    $.each(data,function (key,value) {
        input = $("<input type='hidden'>");
        input.attr({"name":key});
        input.val(value);
        form.append(input);
    });
    $(document.body).append(form);
    form.submit();
}
//重新加载table
function load(obj){
    tableIns.reload({
        where: obj.field
        , page: {
            curr: pageCurr //从当前页码开始
        }
    });
}